$(function(){
    var attendance_datagrid = $("#attendance_datagrid");
    $("#attendance_datagrid").datagrid({
        url:'/attendance/list',
        fit:true,
        fitColumns:true,
        toolbar:"#attendance_buttons",
        pagination:true,
        rownumbers:true,
        singleSelect:true,
        columns:[[
            {field:'employee',title:'姓名',width:100,formatter:formatterName},
            {field:'signInTime',title:'签到时间',width:100},
            {field:'signBackTime',title:'签退时间',width:100},
            {field:'instatus',title:'签到状态',width:100,formatter:formatterIn},
            {field:'backStatus',title:'签退状态',width:100,formatter:formatterBack},
            {field:'attendanceDate',title:'记录日期',width:100}
        ]]
    });
    var cmdObj = {
        signIn:function () {
            $.post("/attendance/signIn",{},function(data){
                console.log(data);
                if(data.success){
                    $.messager.alert("提示","签到成功","info",function () {
                        attendance_datagrid.datagrid("reload");
                    });
                }else{
                    $.messager.alert("提示",data.msg,"error")
                }
            })
        },
        signBack:function () {
            var date = new Date().toLocaleTimeString();
            $.messager.confirm("友情提示","当前时间为:"+ date +"确定签退吗？",function(yes){
                if(yes){
                    $.post("/attendance/signBack",{},function(data){
                        if(data.success){
                            $.messager.alert("提示","签退成功","info");
                            attendance_datagrid.datagrid("reload");
                        }else{
                            $.messager.alert("提示",data.msg,"error");
                        }
                    });
                }
            })
        },
        reload:function(){
            attendance_datagrid.datagrid("reload")
        },
        search:function () {
            attendance_datagrid.datagrid("load", {keyword: $("#keyword").val(),beginTime: $("#beginTime").val(),endTime: $("#endTime").val()});
        }
    };
    //绑定事件
    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        cmdObj[cmd]();
    });
});
function formatterIn(value,row,index){
    if (row.signInTime) {
        return row.instatus ? "<span style='color:#00ee00'>已签到</span>" : "<span style='color:#ff0000'>迟到</span>";
    }
    if (!row.signInTime && !row.signBackTime) {
        return "";
    }
    return "<span style='color:#2b542c'>未签到</span>";
};
function formatterBack(value,row,index){
    if (!row.signInTime && !row.signBackTime) {
        return "";
    }
    if (row.signBackTime) {
        return row.backStatus ? "<span style='color:#ff0000'>早退</span>" : "<span style='color:#00ee00'>已签退</span>";
    }
    return "<span style='color:#2b542c'>未签退</span>";
};
function formatterName(value, row, index) {
    return value.realName;
}